public class HospitalSetEffectiveDateExt {
    String choice = null;
    String hosId = System.currentPageReference().getParameters().get('id');
    List<Hospital__c> target = [select Name, ownerId, class__c, approvaltype__c, approvalstatus__c, Active__c, classchangeto__c, effectivedate__c, ownerchangeto__c, namechangeto__c, comments__c from Hospital__c where Id =: System.currentPageReference().getParameters().get('id')];
    
    public Hospital__c hos {
        get{
            if (hos==null)
                if(target.size() > 0){
                    hos = target[0];
                    Date tmp = date.today();
                    if(tmp.month() <> 12)
                        hos.effectivedate__c = date.newinstance(tmp.year(), tmp.month()+1, 1);
                    else
                        hos.effectivedate__c = date.newinstance(tmp.year()+1, 1, 1);
                }
                else
                    hos = null;
            return hos;
        }
        set;
    }
    
    public PageReference Submit() {
        if(hos.approvalstatus__c <> '审批通过但未生效')
            hos.addError('非法操作：无需设置生效日期');
        else if(hos.effectivedate__c <= date.today())
            hos.addError('生效日期无效');
        else{
            update hos;
            return new ApexPages.StandardController(new Hospital__c(Id = hosId)).view();
        }
        return null;
    }
    
    public PageReference Cancel() {
        return new ApexPages.StandardController(new Hospital__c(Id = hosId)).view();
    }

    static testMethod void HospitalSetEffectiveDateExtTest()
    {
        HospitalSetEffectiveDateExt test = new HospitalSetEffectiveDateExt();
        Hospital__c add = new Hospital__c();
        test.target.add(add);
        
        test.Submit();
        test.Cancel();
    
    }
}